const router=require('koa-router')();
const {User}=require('../models/index');
const {post:postProxy}=require('../proxy/index');
const checkPage=require('../middlewares/checkPage');
const Page=require('../lib/page');

router.get('/',checkPage(),async ctx=>{
    let {loginUser}=ctx.session;
    let [{total,page,posts},noReplies,top10]=await Promise.all([
        postProxy.getList(null,null,{page:ctx.query.p}),//所有话题
        postProxy.getList({comments:{$size:0}},null,{limit:5}),//无人回复的话题
        User.find(null,{name:true,score:true},{skip:0,limit:10,sort:{score:-1}})//积分榜
    ]);

    await ctx.render('postList',{
        title:'Home',
        loginUser,
        posts,
        noReplies:noReplies.posts,
        top10,
        messages:ctx.state.messages,
        success:ctx.flash.success,
        error:ctx.flash.error,
        page:Page(ctx.query,page,total)
    });
});

module.exports=router;